<template>
  <div class="no-ware">
    <MainHeader
      @inputChange="inputChange"
      placeholder="搜索商品名称……"
      title="未入库商品"
    ></MainHeader>

    <div class="control">
      <div class="right">
        <el-button @click="addShopShow" size="small " type="primary"
          >添加商品</el-button
        >
      </div>
    </div>
    <noWarehousingList :searchInput="searchInput"></noWarehousingList>

    <HyTable
      ref="HyTable"
      @myAddShop="myAddShop"
      @handleCurrentChange="handleCurrentChange"
      @tableSearch="tableSearch"
      :tableItems="tableItems"
      :tableData="tableData"
      :totalPage="totalPage"
      :tableSize="tableSize"
      :dialogWidth="dialogWidth"
    >
      <template #price
        ><el-radio-group style="padding: 10px 20px" v-model="PriceType">
          <el-radio :label="1">一口价</el-radio>
          <el-radio :label="2">价格区间</el-radio>
          <el-radio :label="3">显示折扣价</el-radio>
        </el-radio-group></template
      >
      <template #status="scope">
        <strong>{{ ShowStatus(scope.row.ShowStatus) }}</strong>
      </template>
      <template #IsVirtual="scope">
        <strong>{{ IsVirtual(scope.row.IsVirtual) }}</strong>
      </template>
    </HyTable>
  </div>
</template>

<script lang="ts">
import { defineComponent, inject, onMounted, ref } from 'vue'
import { MainHeader } from '@/components/main-header/index'
import noWarehousingList from './cpns/no-warehousing-list.vue'
import { HyTable } from '@/base-ui/table/index'
import tableItems from './config/tableItems-config'
import { goWxProducts, addShopGood } from '@/service/main/main'
import { ElMessage } from 'element-plus'

export default defineComponent({
  components: { MainHeader, noWarehousingList, HyTable },
  setup() {
    // 获取组件ref
    const HyTable = ref()

    onMounted(() => {
      return { HyTable }
    })
    // 刷新页面
    const reload: any = inject('reload')
    // table组件商品搜索
    let tableValue = ref()
    // 价格区间类型
    let PriceType = ref(1)
    // 默认页码
    const currentPage = ref(1)
    // 向上取整 计算总页数
    let totalPage = ref()
    // 获取商品列表
    let tableData = ref()
    // 每页页数
    const pages = 10
    // 导入的商品列表
    let goWxProductsRes: any = []
    async function goList() {
      goWxProductsRes = await goWxProducts(
        currentPage.value,
        pages,
        tableValue.value,
      )
      totalPage.value = Math.ceil(goWxProductsRes.Total / pages) * 100
      tableData.value = goWxProductsRes.List
    }

    // 分页事件
    const handleCurrentChange = async (val: any) => {
      console.log(val)
      currentPage.value = val
      goWxProductsRes = await goWxProducts(
        currentPage.value,
        pages,
        tableValue.value,
      )
      tableData.value = goWxProductsRes.List
    }
    // 显示table组件
    goList()
    const addShopShow = () => {
      HyTable.value.changeVisibility(true)
    }

    // 确认添加
    async function myAddShop(GoodsIds: number[]) {
      if (GoodsIds.length == 0) {
        ElMessage({
          type: 'error',
          message: '请选择商品!',
        })
      }
      await addShopGood(GoodsIds, PriceType.value)
      reload()
      HyTable.value.changeVisibility(false)
    }
    let searchInput = ref()
    // 未入库商品搜索
    const inputChange = (e: any) => {
      // console.log(e)
      searchInput.value = e
    }

    const tableSearch = (e: any) => {
      // console.log(e)
      tableValue.value = e
      goList()
    }
    // 控制长度
    const dialogWidth = '60%'

    // 计算属性
    const ShowStatus = (event: any) => {
      if (event == 0) return '正常'
      if (event == 1) return '失效'
      if (event == 2) return '售馨'
      if (event == 3) return '下架'
    }
    const IsVirtual = (event: any) => {
      if (event == 0) return '实体商品'
      if (event == 1) return '虚拟商品'
    }
    return {
      myAddShop,
      handleCurrentChange,
      goList,
      addShopShow,
      tableSearch,
      inputChange,
      ShowStatus,
      IsVirtual,
      tableItems,
      tableData,
      HyTable,
      searchInput,
      PriceType,
      totalPage,
      dialogWidth,
    }
  },
})
</script>

<style scoped lang="less">
.no-ware {
  .control {
    display: flex;
    justify-content: flex-end;

    .right {
      display: flex;
      align-items: center;
      margin-right: 40px;

      .total-number {
        margin-right: 0.6rem;
        color: #888;
        font-size: 0.14rem;
      }
    }
  }
}
</style>
